package cn.jbolt.core.service.base;

import cn.jbolt.core.base.config.JBoltConfig;
import cn.jbolt.core.db.datasource.JBoltDataSourceUtil;
import cn.jbolt.core.db.datasource.JBoltDatasource;
import cn.jbolt.core.db.util.JBoltDbKit;
import cn.jbolt.core.model.*;
import cn.jbolt.core.service.JBoltGlobalConfigService;
import cn.jbolt.core.service.JBoltPermissionService;
import cn.jbolt.core.service.JBoltUserService;
import com.jfinal.aop.Inject;
import com.jfinal.kit.StrKit;
import com.jfinal.log.Log;

public class JBoltSaasTenantService{
	protected static final Log LOG = Log.getLog(JBoltSaasTenantService.class);
	@Inject
	private JBoltUserService jBoltUserService;
	@Inject
	private JBoltGlobalConfigService jBoltGlobalConfigService;
	@Inject
	private JBoltPermissionService jBoltPermissionService;
	/**
	 * 创建分表+数据初始化 用户 权限 全局配置
	 * @param fromDatasourceConfigName
	 * @param tenantId
	 * @param tenantName
	 * @param tenantSn
	 * @return
	 */
	public boolean creatTenantSeparateCoreTableAndInitDatas(String fromDatasourceConfigName,String tenantId,String tenantName, String tenantSn) {
		boolean success = createTenantSeparateTableByJBoltCoreTable(fromDatasourceConfigName, tenantId,tenantName,tenantSn);
		if(success) {
			jBoltUserService.initTenantSystemAdminUser(tenantId, tenantName, tenantSn);
			jBoltGlobalConfigService.initTenantGlobalConfigs(tenantId, tenantName, tenantSn);
			jBoltPermissionService.initTenantPermissions(tenantId, tenantName, tenantSn);
		}
		return true;

	}

	/**
	 * 创建租户平台主要分表
	 * @param fromDatasourceConfigName
	 * @param tenantId
	 * @param tenantName
	 * @param tenantSn
	 * @return
	 */
	protected boolean createTenantSeparateTableByJBoltCoreTable(String fromDatasourceConfigName,String tenantId,String tenantName, String tenantSn) {
		return createTenantSeparateTableByJBoltCoreTable(fromDatasourceConfigName,JBoltConfig.SAAS_TENANT_DATASOURCE_CONFIG_NAME, tenantId,tenantName,tenantSn);
	}

	/**
	 * 创建租户平台主要分表
	 * @param fromDatasourceConfigName
	 * @param toDatasourceConfigName
	 * @param tenantId
	 * @param tenantName
	 * @param tenantSn
	 * @return
	 */
	protected boolean createTenantSeparateTableByJBoltCoreTable(String fromDatasourceConfigName,String toDatasourceConfigName,String tenantId,String tenantName, String tenantSn) {
		if(StrKit.isBlank(toDatasourceConfigName)) {
			throw new RuntimeException("未指定生成到哪个数据源");
		}
		if(StrKit.hasBlank(tenantId,tenantName,tenantSn)) {
			throw new RuntimeException("未指定分表租户ID NAME SN");
		}
		LOG.debug("正在创建租户:["+tenantName+":"+tenantSn+"]的核心表分表....");
		JBoltDatasource toJboltDatasource = JBoltDataSourceUtil.me.getJBoltDatasource(toDatasourceConfigName);
		if(toJboltDatasource == null){
			throw new RuntimeException("数据源配置【"+toDatasourceConfigName+"】加载异常 无法获取jboltDataSource");
		}
		JBoltDatasource fromJboltDatasource = JBoltDataSourceUtil.me.getJBoltDatasource(fromDatasourceConfigName);
		if(fromJboltDatasource == null){
			throw new RuntimeException("数据源配置【"+fromDatasourceConfigName+"】加载异常 无法获取jboltDataSource");
		}
		//1、jb_user
		JBoltDbKit.createTenantSeparateTable(fromJboltDatasource,toJboltDatasource, User.class,tenantId);
		//2、jb_role
		JBoltDbKit.createTenantSeparateTable(fromJboltDatasource,toJboltDatasource, Role.class,tenantId);
		//3、jb_permission
		JBoltDbKit.createTenantSeparateTable(fromJboltDatasource,toJboltDatasource, Permission.class,tenantId);
		//4、jb_role_permission
		JBoltDbKit.createTenantSeparateTable(fromJboltDatasource,toJboltDatasource, RolePermission.class,tenantId);
		//5、jb_dept
		JBoltDbKit.createTenantSeparateTable(fromJboltDatasource,toJboltDatasource, Dept.class,tenantId);
		//6、jb_post
		JBoltDbKit.createTenantSeparateTable(fromJboltDatasource,toJboltDatasource, Post.class,tenantId);
		//7、jb_global_config
		JBoltDbKit.createTenantSeparateTable(fromJboltDatasource,toJboltDatasource, GlobalConfig.class,tenantId);
		//8、jb_global_config
		JBoltDbKit.createTenantSeparateTable(fromJboltDatasource,toJboltDatasource, GlobalConfigType.class,tenantId);
		//9、jb_user_config
		JBoltDbKit.createTenantSeparateTable(fromJboltDatasource,toJboltDatasource, UserConfig.class,tenantId);
		//10、jb_online_user
		JBoltDbKit.createTenantSeparateTable(fromJboltDatasource,toJboltDatasource, OnlineUser.class,tenantId);
		//11、jb_login_log
		JBoltDbKit.createTenantSeparateTable(fromJboltDatasource,toJboltDatasource, LoginLog.class,tenantId);
		//12、jb_remote_login_log
		JBoltDbKit.createTenantSeparateTable(fromJboltDatasource,toJboltDatasource, RemoteLoginLog.class,tenantId);
		//13、jb_system_log
		JBoltDbKit.createTenantSeparateTable(fromJboltDatasource,toJboltDatasource, SystemLog.class,tenantId);
		//14、jb_topnav
		JBoltDbKit.createTenantSeparateTable(fromJboltDatasource,toJboltDatasource, Topnav.class,tenantId);
		//15、jb_topnav_menu
		JBoltDbKit.createTenantSeparateTable(fromJboltDatasource,toJboltDatasource, TopnavMenu.class,tenantId);
		LOG.debug("创建租户:["+tenantName+":"+tenantSn+"]的核心表分表 完成！....");
		return true;
	}





}
